package com.leejxx.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leejxx.health.dao.CheckGroupDao;
import com.leejxx.health.dao.CheckItemDao;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.exception.BusinessRuntimeException;
import com.leejxx.health.pojo.CheckItem;
import com.leejxx.health.service.CheckItemService;
import com.leejxx.health.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @Author LeeJx
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @date 2020/12/30 11:31
 * @Version 1.0
 */
@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemDao.findByCodeOrName(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());



//        String queryString = queryPageBean.getQueryString();
//        if (null == queryString || queryString.length()==0) {
//            Integer offset = queryPageBean.getOffset();
//            Integer pageSize = queryPageBean.getPageSize();
//            List<CheckItem> checkItems = checkItemDao.findByPage(offset, pageSize);
//
//            Long total = checkItemDao.findTotal();
//            return new PageResult(total, checkItems);
//        } else {
//            List<CheckItem> checkItems = checkItemDao.findByCodeOrName(queryString);
//
//            Long total = checkItemDao.findByCodeOrNameTotal(queryString);
//            return new PageResult(total, checkItems);
//        }
    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
        List<Integer> ids = checkItemDao.selectCheckGroupIdByCheckItemId(checkItem.getId());
        for (Integer id : ids) {
            List<Integer> list = checkGroupDao.selectSetmealIdByCheckGroupId(id);
            for (Integer integer : list) {
                RedisUtils.flushRedis(redisTemplate,integer);
            }
        }
    }

    @Override
    public CheckItem findByCode(Integer code) {
        return checkItemDao.findByCode(code);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
        List<Integer> ids = checkItemDao.selectCheckGroupIdByCheckItemId(checkItem.getId());
        for (Integer id : ids) {
            List<Integer> list = checkGroupDao.selectSetmealIdByCheckGroupId(id);
            for (Integer integer : list) {
                RedisUtils.flushRedis(redisTemplate,integer);
            }
        }
    }

    @Override
    public void delete(Integer code) {
        Long count = checkItemDao.findCountOfCheckGroupByCode(code);
        if (count > 0) {
            throw new BusinessRuntimeException("当前检测项有数据，无法删除");
        }
        checkItemDao.delete(code);
        List<Integer> ids = checkItemDao.selectCheckGroupIdByCheckItemId(code);
        for (Integer id : ids) {
            List<Integer> list = checkGroupDao.selectSetmealIdByCheckGroupId(id);
            for (Integer integer : list) {
                RedisUtils.flushRedis(redisTemplate,integer);
            }
        }
    }
}
